﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using HZ.Supervise.IBLL;
using HZ.Supervise.Model;
using HZ.Supervise.Model.Enum;
using HZ.Supervise.Model.View;
using System.Data;
using HZ.Supervise;

namespace HZ.Supervise.Web.Hospital
{
    public partial class AreaStatistics_DZ : BasePage
    {
        public AreaStatistics_DZ()
        {
            //this.ModuleNo = Module.医院统计分析.ToString();
            //this.PageOperateType = OperateType.地区汇总统计.ToString();
        }

        IBLL_User iu = BLLFactory.BLL_User();
        KeyOperationLogJG kj;
        IBLL_Company ic = HZ.Supervise.BLLFactory.IBLL_Company();
        HZ.Supervise.IBLL.IExecuteContext i = HZ.Supervise.BLLFactory.ExecuteContext();
        SuperviseQuery vs = new SuperviseQuery();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                //ddlArea.InitAreaID = CurrentAccount.GetUser().Org.Area.AreaID;
                //BindData();
            }
        }

        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void AspNetPager_CompanyPS_PageChanged(object sender, EventArgs e)
        {
            //AspNetPager_CompanyPS.CurrentPageIndex = 1; 
            BindData();
        }

        /// <summary>
        /// 绑定数据 
        /// </summary>
        public void BindData()
        {
            string orderby = this.OrderFileds.SelectedValue + "  " + this.OrderType.SelectedValue;//排序字段与方式
            vs = Queryby();
            int t = 0;
            System.Data.DataTable dt = i.GetStatistics_DZ(4, orderby, vs, this.AspNetPager_CompanyPS.CurrentPageIndex, AspNetPager_CompanyPS.PageSize, out t);
            AspNetPager_CompanyPS.RecordCount = t;
            this.gvwPSLInfo.PageSize = AspNetPager_CompanyPS.PageSize;
            this.gvwPSLInfo.DataSource = dt;
            this.gvwPSLInfo.DataBind();
            dt = i.GetStatistics_DZ(4, orderby, vs, 1, 0, out t);
            double PurchaseAmountTotal = 0;
            double WareHouseAmountTotal = 0;
            double DistributeAmountTotal = 0;
            //double ReturnAmountTotal = 0;
            for (int j = 0; j < dt.Rows.Count; j++)
            {
                PurchaseAmountTotal += Convert.ToDouble(dt.Rows[j]["PurchaseAmount"]);
                WareHouseAmountTotal += Convert.ToDouble(dt.Rows[j]["WarehouseAmount"]);
                DistributeAmountTotal += Convert.ToDouble(dt.Rows[j]["DistributeAmount"]);
                //ReturnAmountTotal += Convert.ToDouble(dt.Rows[j]["ReturnAmount"]);
            }

            lblMemo.Text = "采购金额是" + PurchaseAmountTotal.ToString("0.00") + "元 配送金额是" + DistributeAmountTotal.ToString("0.00") + "元 入库金额是" + WareHouseAmountTotal.ToString("0.00") 
                //+ "元 退货金额是" + ReturnAmountTotal.ToString("0.00") 
                + "元";
            AddLog(string.Format("查询地市低值汇总统计数据!"));

            // Session[sessionId] = vs;
        }
        protected void btnQuery_Click(object sender, EventArgs e)
        {
            AspNetPager_CompanyPS.CurrentPageIndex = 1;
        }

        protected void gvwInfo_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            Control cmdControl = e.CommandSource as Control;
            GridViewRow row = cmdControl.NamingContainer as GridViewRow;
            string str = row.Cells[1].Text;
            if (str != "总计")
            {
                vs = Queryby();
                vs.BackPage = "area";
                string sessionId = Guid.NewGuid().ToString();
                vs.AreaID = e.CommandArgument.ToString().Split('|')[0];
                vs.AreaName = e.CommandArgument.ToString().Split('|')[1];
                Session[sessionId] = vs;

                if (e.CommandName == "lbnP")
                {
                    Response.Redirect("../Enterprise/PurchaseAmountDetail_DZ.aspx?sesid=" + sessionId);
                }
                if (e.CommandName == "lbnD")
                {
                    Response.Redirect("../Enterprise/DistributeAmountDetail_DZ.aspx?sesid=" + sessionId);
                }
                if (e.CommandName == "lbnW")
                {
                    Response.Redirect("../Enterprise/WarehouseAmountDetail_DZ.aspx?sesid=" + sessionId);
                }
                //if (e.CommandName == "lbnS")
                //{
                //    Response.Redirect("../Enterprise/SellAmountDetail.aspx?sesid=" + sessionId);
                //}
                //if (e.CommandName == "lbnR")
                //{
                //    Response.Redirect("../Enterprise/ReturnAmountDetail_DZ.aspx?sesid=" + sessionId);

                //}
            }
        }
        /// <summary>
        /// 分页    县区 
        /// </summary>
        /// <param name="sender">事件发送源</param>
        /// <param name="e">事件参数</param>
        protected void CompanyPSL_PS_PageChanged(object sender, EventArgs e)
        {
            BindData();

        }
        /// <summary>
        /// 导出 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnExcel_Click(object sender, EventArgs e)
        {
            string orderby = this.OrderFileds.SelectedValue + "  " + this.OrderType.SelectedValue;//排序字段与方式
            //SuperviseQuery vs = new SuperviseQuery();
            vs = Queryby();
            int t = 0;
            System.Data.DataTable dt = i.GetStatistics_DZ(4, orderby, vs, 1, 0, out t);
            AddLog(string.Format("导出地市低值汇总统计数据成功!"));
            ExportFile.Instance.ExportData(dt, gvwPSLInfo, this.Page);
        }

        /// <summary>
        /// 查询条件
        /// </summary>
        /// <returns></returns>
        private SuperviseQuery Queryby()
        {
            //时间
            vs.Append += string.Format(" 1=1 ");
            DateTime startime;
            DateTime endtime;
            if (!string.IsNullOrEmpty(txtStratTime.Text.Trim()))
            {
                startime = (DateTime)new HZ.Common.ConvertHelper(txtStratTime.Text.Trim()).ToDateTime;//开始时间
                vs.Append += string.Format(" and AmountTime>= dbo.fun_GetYearMonth('{0}')", startime);
            }
            if (!string.IsNullOrEmpty(txtEndTime.Text.Trim()))
            {
                endtime = (DateTime)new HZ.Common.ConvertHelper(txtEndTime.Text.Trim()).ToDateTime;//结束时间
                vs.Append += string.Format(" and AmountTime <= dbo.fun_GetYearMonth('{0}') ", endtime);
            }
            //string timeFrom = "1900-01-01 00:00:00";//起始时间
            //string timeTo = "2444-12-30 23:59:59";//结束时间
            //if (!string.IsNullOrEmpty(txtStratTime.Text))
            //{

            //    vs.SubmiTimeFrom = Convert.ToDateTime(txtStratTime.Text.Trim());
            //}
            //else
            //{
            //    vs.SubmiTimeFrom = Convert.ToDateTime(timeFrom);
            //}
            //if (!string.IsNullOrEmpty(txtEndTime.Text))
            //{
            //    vs.SubmiTimeTo = Convert.ToDateTime(txtEndTime.Text.Trim()).AddDays(1);

            //}
            //else
            //{
            //    vs.SubmiTimeTo = Convert.ToDateTime(timeTo);
            //}
            //if (vs.SubmiTimeFrom > vs.SubmiTimeTo)
            //{
            //    HZ.Web.WebUtil.Alert("开始时间不能大于结束时间");
            //    vs.SubmiTimeFrom = Convert.ToDateTime(timeFrom);
            //    vs.SubmiTimeTo = Convert.ToDateTime(timeTo);
            //}
            //if (ddlIsGuoChan.SelectedValue != "全部")
            //{
            //    vs.IsGuoChan = ddlIsGuoChan.SelectedValue;
            //}
            //vs.AreaID = ddlArea.SelectedAreaID;
            return vs;
        }

        /// <summary>
        /// 添加日志
        /// </summary>
        /// <param name="msg"></param>
        private void AddLog(string msg)
        {
            kj = new KeyOperationLogJG();
            kj.OperationLogSerialID = Guid.NewGuid().ToString();
            kj.Operator = CurrentAccount.GetUser().UserID;
            kj.OrgID = CurrentAccount.GetUser().Org.OrgID;
            kj.OperateTime = DateTime.Now;
            kj.OperateMessage = msg;
            iu.AddKeyOperationLog(kj);
        }
    }
}